unit uCnrRunningCtx;

{
  two running context, for local and remote.

}
interface
uses DrcpSvc,types,uPlugInIntf,IniFiles,SysUtils;

const
  //below const used in ini configure file for key, and default value
  RS_KEY_RunningMode='RunningMode';
  RS_KEY_Debug='Debug';
  RS_KEY_PluginPath='PluginPath';
  RS_KEY_GlobalVariableFileName='GlobalVariableFileName';
  RS_KEY_ClientConfigFileName='ClientConfigFileName';
  RS_KEY_AppServerSoapAddr = 'AppServerSoapAddr';

  RS_DEFAULT_RunningMode='Offline';
  RS_DEFAULT_Debug='false';
  RS_DEFAULT_PluginPath ='Plugin';
  RS_DEFAULT_GlobalVariableFileName='global_variable.ini';
  RS_DEFAULT_ClientConfigFileName='client_config.ini';
  RS_DEFAULT_AppServerSoapAddr = 'http://not-exist.com/no-service';

type

  TContainerRunningContext=class

  public
    function GetGlobalVariable(AName : string):string;virtual;abstract;

    function OnCheckPassword(AUserName : string; APassword : string):boolean;virtual;abstract;
    function Plugin_GetVersionByName(APluginName : string):string;virtual;abstract;
    function Plugin_GetDataByName(APluginName : string):TByteDynArray;virtual;abstract;

    function GetHostModuleParams(const AHost, AModule: string): string;virtual;abstract;
    procedure SetHostModuleParams(const AHost, AModule, AParams: string);virtual;abstract;

    function GetFunctionAuthorization(const AUserId, AFunctionPath : string):boolean;virtual;abstract;

    procedure AddLog(const userId: String; const computerName: String; const source: String; const type_: Integer; const category: String; const action: String; const comment: String);virtual;abstract;
  end;

  TContainerRunningContextLocal=class(TContainerRunningContext)
  private
    FConfigFileName : string;
    FContainer : IContainer;
    FStartupPlugins : string;

  public
    constructor Create(AConfigFileName : string; AContainer : IContainer);
    procedure LoadConfig;

    function GetGlobalVariable(AName : string):string;override;

    function OnCheckPassword(AUserName : string; APassword : string):boolean;override;

    function GetHostModuleParams(const AHost, AModule: string): string;override;
    procedure SetHostModuleParams(const AHost, AModule, AParams: string);override;

    function GetFunctionAuthorization(const AUserId, AFunctionPath : string):boolean;override;

    procedure AddLog(const userId: String; const computerName: String; const source: String; const type_: Integer; const category: String; const action: String; const comment: String);override;
  end;

  TContainerRunningContextRemote=class(TContainerRunningContext)
  private
    FSoap : DrcpService;
  public
    constructor Create(ASoapPath : string);


    function GetGlobalVariable(AName : string):string;override;

    function OnCheckPassword(AUserName : string; APassword : string):boolean;override;

    function Plugin_GetVersionByName(APluginName : string):string;override;
    function Plugin_GetDataByName(APluginName : string):TByteDynArray;override;

    function GetHostModuleParams(const AHost, AModule: string): string;override;
    procedure SetHostModuleParams(const AHost, AModule, AParams: string);override;

    function GetFunctionAuthorization(const AUserId, AFunctionPath : string):boolean;override;

    procedure AddLog(const userId: String; const computerName: String; const source: String; const type_: Integer; const category: String; const action: String; const comment: String);override;
  end;



implementation

uses Classes;

{ TContainerRunningContextRemote }

procedure TContainerRunningContextRemote.AddLog(const userId, computerName,
  source: String; const type_: Integer; const category, action,
  comment: String);
begin
  FSoap.Log_Add(userId,computerName,source,type_,category,action,comment);
end;

constructor TContainerRunningContextRemote.Create(ASoapPath: string);
begin
  FSoap := GetDrcpService(false,ASoapPath);
end;

function TContainerRunningContextRemote.GetFunctionAuthorization(
  const AUserId, AFunctionPath: string): boolean;
begin
  Result :=FSoap.FunctionAuthorization_IsUserAuthorizedByPath(AUserId,AFunctionPath);
end;

function TContainerRunningContextRemote.GetHostModuleParams(const AHost,
  AModule: string): string;
begin
  Result := FSoap.ClientConfig_GetContent(AHost,AModule);
end;

function TContainerRunningContextRemote.GetGlobalVariable(AName: string): string;
begin
  Result := FSoap.GlobalVariable_GetValue(AName);
end;

function TContainerRunningContextRemote.OnCheckPassword(AUserName,
  APassword: string): boolean;
begin
 Result := FSoap.Login(AUserName,APassword);
end;

function TContainerRunningContextRemote.Plugin_GetDataByName(
  APluginName: string): TByteDynArray;
begin
  Result := FSoap.Plugin_GetDataByName(APluginName);
end;

function TContainerRunningContextRemote.Plugin_GetVersionByName(
  APluginName: string): string;
begin
  Result := FSoap.Plugin_GetVersionByName(APluginName);
end;

procedure TContainerRunningContextRemote.SetHostModuleParams(const AHost,
  AModule, AParams: string);
begin
  FSoap.ClientConfig_SetContent(AHost,AModule,AParams);
end;

{ TContainerRunningContextLocal }

procedure TContainerRunningContextLocal.AddLog(const userId, computerName,
  source: String; const type_: Integer; const category, action,
  comment: String);
begin
  //write to txt file

end;

constructor TContainerRunningContextLocal.Create(AConfigFileName: string; AContainer : IContainer);
begin
  FConfigFileName := AConfigFileName;
  FContainer := AContainer;
  //
  LoadConfig;  
end;

function TContainerRunningContextLocal.GetFunctionAuthorization(
  const AUserId, AFunctionPath: string): boolean;
begin
  Result := true;
end;

function TContainerRunningContextLocal.GetHostModuleParams(const AHost,
  AModule: string): string;
var
  lFileName : string;
begin
  lFileName := format('%s%s',[FContainer.Variable[RS_APPLICATION_PATH],FContainer.Variable[RS_KEY_ClientConfigFileName]]);
  with TIniFile.Create(lFileName) do
  begin
    Result := ReadString('variables',AModule,'');

    Free;
  end;
  Result := StringReplace(Result,'<BR>',#$D#$A,[rfReplaceAll, rfIgnoreCase]);
end;

function TContainerRunningContextLocal.GetGlobalVariable(AName: string): string;
var
  lFileName : string;
begin

  lFileName := format('%s%s',[FContainer.Variable[RS_APPLICATION_PATH],FContainer.Variable[RS_KEY_GlobalVariableFileName]]);
  with TIniFile.Create(lFileName) do
  begin
    Result := ReadString('variables',AName,'');

    Free;
  end;

end;

procedure TContainerRunningContextLocal.LoadConfig;
begin


end;

function TContainerRunningContextLocal.OnCheckPassword(AUserName,
  APassword: string): boolean;
begin
  Result := true;
end;

procedure TContainerRunningContextLocal.SetHostModuleParams(const AHost,
  AModule, AParams: string);
var
  S : string;
  lFileName : string;
begin
  S := StringReplace(AParams,#$D#$A,'<BR>',[rfReplaceAll, rfIgnoreCase]);

  lFileName := format('%s%s',[FContainer.Variable[RS_APPLICATION_PATH],FContainer.Variable[RS_KEY_ClientConfigFileName]]);
  with TIniFile.Create(lFileName) do
  begin
    WriteString('variables',AModule,AParams);

    Free;
  end;

end;

end.
